// Console Program for PCMod (CLIENT)

PROG.PName = "Console"
PROG.Author = "[GU]|ZLC|thomasfn"

PROG.Icon = "icons/ico_console"

PROG.NiceName = "Console"

function PROG:BuildWindow( WindowName, Desktop, Object )
	// Here we build the derma window.
	// Note the Desktop string is needed for every operation involving derma.
	if (WindowName == "Main") then
		// Add a title label
		//DInterface:AddLabel( "lblTitle", Desktop, "PC Console", 5, 5 )
		
		// Add the console area
		//DInterface:AddHTML( "htcon", Desktop, "", 5, 25, Object:GetWide()-10, Object:GetTall()-55 )
		self.ChatArea = vgui.Create( "ChatView" )
		self.ChatArea:SetParent( Object )
		self.ChatArea:SetPos( 5, 25 )
		self.ChatArea:SetSize( Object:GetWide()-10, Object:GetTall()-55 )
		
		// Add the command textbox and send button
		DInterface:AddLabel( "lblInfo", Desktop, "Command:", 5, Object:GetTall()-25 )
		DInterface:AddTextbox( "txtCom", Desktop, "", 105, Object:GetTall()-25, Object:GetWide()-190, 20 )
		DInterface:AddButton( "btnSend", Desktop, "Send", Object:GetWide()-80, Object:GetTall()-25, 70, 20, function()
			local com = DInterface:GetTextboxValue( "txtCom", Desktop )
			LocalPlayer():ConCommand( "pc_command " .. com )
			DInterface:SetTextboxText( "txtCom", Desktop, "" )
			C_PRO:AddConsoleMessage( "-" .. com )
			C_PRO:UpdateConsole( Desktop )
		end )
		
		self.Desktop = Desktop
		
	end
	
	// Add a quit button
	DInterface:AddButton( "btnQuit", Desktop, "X", Object:GetWide()-20, 0, 20, 20, function() SendCommand( "quit" ); end )
	
	// Update our console area
	self:UpdateConsole( Desktop )
end

function PROG:ClearWindow( Desktop )
	// Here we destroy EVERY derma control that MIGHT exist.
	DInterface:RemoveLabel( "lblTitle", Desktop )
	DInterface:RemoveHTML( "htcon", Desktop )
	
	DInterface:RemoveLabel( "lblInfo", Desktop )
	DInterface:RemoveTextbox( "txtCom", Desktop )
	DInterface:RemoveButton( "btnSend", Desktop )
	
	if (self.ChatArea) then
		self.ChatArea:Remove()
		self.ChatArea = nil
	end
	
	DInterface:RemoveButton( "btnQuit", Desktop )
end

// Data[1] = Tag
// Data[2] = Content
function PROG:DataRecieved( Desktop, Data )
	// The server has sent us some data
end

// Custom Functions
function PROG:UpdateConsole( Desktop )
	local Text = ""
	local pc = CurrentPC
	local msgcount = pc:GetNWInt( "console_msgs" )
	if (msgcount == 0) then return end
	for n=1,msgcount do
		local str = pc:GetNWString( "console_msg_" .. tostring(n) )
		if (str) then Text = Text .. str .. "#" end
	end
	self.ChatArea:SetChatText( Text, "#" )
end

function PROG:AddConsoleMessage( message )
	// Get some message details
	local pc = CurrentPC
	msgcnt = pc:GetNWInt( "console_msgs" )
	msgid = msgcnt+1
	
	// Add the message to the PC
	pc:SetNWInt( "console_msgs", msgid )
	pc:SetNWString( "console_msg_" .. tostring(msgid), message )
end

function PROG:Paint( x, y, w, h )
	draw.SimpleText( self.NiceName, "SmallPCText", x + (w*0.05), y + (h*0.05), Color( 255, 255, 255 ), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP )
end